package team.se.tms.controller.course;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Regex;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.Page;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.CourseInfo;
import team.se.tms.service.CourseInfoService;
import team.se.tms.util.ResultUtil;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by argentum on 2018/11/28.
 */
@Controller
@RequestMapping("course")
public class CourseInfoController extends BaseController{

    @Autowired
    CourseInfoService courseInfoService;

    /**
     * 根据人才培养方案id获取课程信息
     *
     * @param talentTrainingProgramId 人才培养方案id
     * @param page 分页
     * @return json字符串
     * @throws Exception 异常
     * @create on 2019/3/21 by winkyle
     */
    @RequestMapping("search_course_infos")
    @ResponseBody
    public String searchCourseInfos(@SpecialCheck("Pk")String talentTrainingProgramId,Page page) throws Exception{
        return page.getDataTabelJson(courseInfoService.listCourseInfos(talentTrainingProgramId,page));
    }


    /**
     * @create on 2018/11/29 by argentum
     * @update on 2019/5/6 by argentum
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("toAddCourse")
    @ResponseBody
    public Object toAddCourse(@Param("talentTrainingProgramId") String talentTrainingProgramId) throws Exception{
        ModelAndView mv = this.getModelAndView();
        System.out.println("talentTrainingProgramId============="+talentTrainingProgramId);
        mv.addObject("talentTrainingProgramId",talentTrainingProgramId);
        mv.setViewName("course/add_course");
        return mv;
    }

    /**
     * @create on 2018/11/29 by argentum
     * @update on 2018/11/29 by argentum
     *
     * @param courseInfo
     * @return
     * @throws Exception
     */


    @RequestMapping("addCourse")
    @ResponseBody
    public Object addCourseInfo(CourseInfo courseInfo) throws Exception {
        System.out.println("add============="+courseInfo);
        boolean result = courseInfoService.saveCourseInfo(courseInfo);
        if(!result){
            throw new InsertException();
        }
        return  ResultUtil.success("添加成功");
    }

    /**
     * 跳转到修改课程页面
     * @param courseId
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping("toEditCourse")
    public ModelAndView toModifyCourse(@Regex("^[A-Za-z0-9]{16}$") String courseId, HttpSession session) throws Exception{
        ModelAndView mv = this.getModelAndView();
        //从session中获取labelLibDetails，取到id 等于要修改的标签库的id
        List<CourseInfo> courseInfos = (List<CourseInfo>)session.getAttribute("courseInfos");
        CourseInfo courseInfo = new CourseInfo();
        if(courseInfos !=null && courseInfos.size()>0){
            for(CourseInfo item:courseInfos){
                if(item.getCourseId().equals(courseId)){
                    courseInfo = item;
                    break;
                }
            }
        }
        System.out.println("=============="+courseInfo);
        mv.addObject("courseInfo",courseInfo);
        mv.setViewName("course/modify_course");
        return mv;
    }

    /**
     * 修改课程信息
     * @param courseInfo
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping("editCourseInfo")
    @ResponseBody
  //  @CheckFormat({"talentTrainingProgramId"})
    public Object editCourseInfo(CourseInfo courseInfo) throws Exception {
        System.out.println("============="+courseInfo);
        boolean result = courseInfoService.modifyCourseInfo(courseInfo);
        if(result == true){
            return  ResultUtil.success("课程修改成功");
        }
        throw new UpdateException("课程修改失败");
    }


    /**
     * 删除课程
     *
     * @param  courseId 课程id
     * @return Object 删除结果
     * @throws Exception 异常
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping("dropCourse")
    @ResponseBody
    @CheckFormat
    public Object dropCourseInfo(@Regex("^[A-Za-z0-9]{16}$") String courseId) throws Exception{
        int result = courseInfoService.removeCourseInfo(courseId);
        if(result == 0){
            throw new DeleteException("删除失败");
        }else if(result == -1){
            throw new DeleteException("该课程已被使用，不允许删除");
        }
        return  ResultUtil.success("删除成功");
    }
}
